#
# Copyright (c) 2015-2020 CNRS INRIA
# Copyright (c) 2015 Wandercraft, 86 rue de Paris 91400 Orsay, France.
#

IF(BUILD_WITH_URDF_SUPPORT)
  LIST(APPEND ${PROJECT_NAME}_SOURCES
    parsers/urdf/model.cpp
    parsers/urdf/geometry.cpp
    parsers/urdf/utils.cpp
  )
ENDIF()

LIST(APPEND ${PROJECT_NAME}_SOURCES utils/file-explorer.cpp)

# Extract the compile definitions of the project for export
GET_DIRECTORY_PROPERTY(CURRENT_COMPILE_DEFINITIONS COMPILE_DEFINITIONS)

# Create target libpinocchio.so
ADD_LIBRARY(${PROJECT_NAME} SHARED ${HEADERS} ${${PROJECT_NAME}_SOURCES})
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES
  LINKER_LANGUAGE CXX
  INSTALL_RPATH "\$ORIGIN")
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PUBLIC ${CURRENT_COMPILE_DEFINITIONS})

# Create header-only target
ADD_LIBRARY(${PROJECT_NAME}_headers INTERFACE)

# Exclude parts from hppfcl which is not header only
IF(BUILD_WITH_HPP_FCL_SUPPORT)
  LIST(REMOVE_ITEM CURRENT_COMPILE_DEFINITIONS PINOCCHIO_WITH_HPP_FCL)
ENDIF(BUILD_WITH_HPP_FCL_SUPPORT)

TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME}_headers INTERFACE ${CURRENT_COMPILE_DEFINITIONS})
MODERNIZE_TARGET_LINK_LIBRARIES(${PROJECT_NAME}_headers SCOPE INTERFACE
  TARGETS Eigen3::Eigen
  INCLUDE_DIRS ${EIGEN3_INCLUDE_DIR})
MODERNIZE_TARGET_LINK_LIBRARIES(${PROJECT_NAME}_headers SCOPE INTERFACE
  TARGETS Boost::filesystem Boost::system Boost::serialization
  LIBRARIES ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_SERIALIZATION_LIBRARY}
  INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME}_headers INTERFACE $<INSTALL_INTERFACE:include>)

IF(MSVC)
  TARGET_COMPILE_OPTIONS(${PROJECT_NAME} PUBLIC "/bigobj")
ENDIF()

SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
IF(BUILD_WITH_COMMIT_VERSION)
  TAG_LIBRARY_VERSION(${PROJECT_NAME})
ENDIF(BUILD_WITH_COMMIT_VERSION)
IF(WIN32)
  TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PRIVATE -DNOMINMAX)
ENDIF(WIN32)

MODERNIZE_TARGET_LINK_LIBRARIES(${PROJECT_NAME} SCOPE PUBLIC
  TARGETS Eigen3::Eigen
  INCLUDE_DIRS ${EIGEN3_INCLUDE_DIR})
MODERNIZE_TARGET_LINK_LIBRARIES(${PROJECT_NAME} SCOPE PUBLIC
  TARGETS Boost::filesystem Boost::system Boost::serialization
  LIBRARIES ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_SERIALIZATION_LIBRARY}
  INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC $<INSTALL_INTERFACE:include>)

# Special care of urdfdom version
IF(urdfdom_FOUND)
  IF(${urdfdom_VERSION} VERSION_LESS "0.3.0")
    TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PRIVATE -DPINOCCHIO_URDFDOM_COLLISION_WITH_GROUP_NAME)
  ENDIF(${urdfdom_VERSION} VERSION_LESS "0.3.0")

  # defines types from version 0.4.0
  IF(NOT ${urdfdom_VERSION} VERSION_LESS "0.4.0")
    TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PRIVATE -DPINOCCHIO_URDFDOM_TYPEDEF_SHARED_PTR)
  ENDIF(NOT ${urdfdom_VERSION} VERSION_LESS "0.4.0")

  # std::shared_ptr appears from version 1.0.0
  IF(${urdfdom_VERSION} VERSION_GREATER "0.4.2")
    TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME} PRIVATE -DPINOCCHIO_URDFDOM_USE_STD_SHARED_PTR)
  ENDIF(${urdfdom_VERSION} VERSION_GREATER "0.4.2")

  MODERNIZE_TARGET_LINK_LIBRARIES(${PROJECT_NAME} SCOPE PUBLIC
    TARGETS urdfdom::urdf_parser
    LIBRARIES ${urdfdom_LIBRARIES}
    INCLUDE_DIRS ${urdfdom_INCLUDE_DIRS})
ENDIF(urdfdom_FOUND)

IF(hpp-fcl_FOUND)
  TARGET_LINK_LIBRARIES(${PROJECT_NAME} PUBLIC hpp-fcl::hpp-fcl)
ENDIF(hpp-fcl_FOUND)

ADD_HEADER_GROUP(HEADERS)
ADD_SOURCE_GROUP(${PROJECT_NAME}_SOURCES)

INSTALL(TARGETS ${PROJECT_NAME}
  EXPORT ${TARGETS_EXPORT_NAME}
  LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})

INSTALL(TARGETS ${PROJECT_NAME}_headers
  EXPORT ${TARGETS_EXPORT_NAME}
  LIBRARY DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_FULL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
